<?php
/*
*yf Echa
*2016年8月13日下午2:59:03
*AdController
*/
namespace SDXApi\Controller;
use SDXApi\Service\OrderService;
use Think\Controller;
class AdController extends AppController {

    public function _initialize(){
        parent::_initialize();
    }

    /*
     * 获取广告位
     * */
    public function ad_location(){
        $ad_position=D('Adposition')->get_ad_location();
        if($ad_position){
            returnApiSuccess(array('list'=>$ad_position));
        }else{
            returnApiError('无数据');
        }
    }

    /*
     * 广告列表
     * */
    public function ad_list(){

        //若抢红包开始，只读最近接口缓存
        $redWarTime=S('redWarTime');
        if($redWarTime && $redWarTime){

        }

        $position_id=I('post.position_id');
        $is_app = I('post.is_app');
        $page=I('post.page');
        $ad=D('Ad')->get_adlist($page,$position_id);

        S('redWarReadPage_ad_list',$ad,1800);

        if($ad){
            returnApiSuccess($ad);
        }else{
            if($is_app==1){
                returnApiSuccess('','无数据');
        }
            returnApiError('无数据');
        }
    }



    /*
     * 获取广告位
     * */
    public function ad_location2(){
        $goods_category=I('post.goods_category');
        $ad_position=D('Adposition')->get_ad_location2($goods_category);
        if($ad_position){
            returnApiSuccess(array('list'=>$ad_position));
        }else{
            returnApiError('无数据');
        }
    }

    /*
     * 广告列表
     * */
    public function ad_list2(){
        $position_id=I('post.position_id');
        $page=I('post.page');
        $ad=D('Ad')->get_adlist2($page,$position_id);
        if($ad){
            returnApiSuccess($ad);
        }else{
            returnApiError('无数据');
        }
    }

    /**
     * 获取所有分类广告
     */
    public function get_category_ad(){

        $sql = "SELECT ad.ad_name,ad.ad_link,c.cat_id,c.cat_name,ad.ad_code
                FROM sdx_category AS c,sdx_ad_position AS ap,sdx_ad AS ad
                WHERE 
                c.cat_id = ap.goods_category AND ap.position_id = ad.position_id AND ad.enabled= 1 AND c.parent_id =  0 AND c.is_show = 1
                AND ad.start_time < ".gmtime()." AND ad.end_time > ".gmtime();
        //dump($sql);
        $goods_shelves = M()->query($sql);

        if($goods_shelves){
            $data=array('list'=>$goods_shelves);
            returnApiSuccess($data,"已返回");
        }else{
            returnApiError("无数据");
        }
    }

    /**
     * @author yangzhic
     */
    public function get_order_share_ad(){
        $ad_data = array();

        $order_id = I('post.order_id',0,'int');
        $uid = I('post.uid',0,'int');

        if(empty($ad_data) && !empty($order_id)){
            $ad_data = $this->women_day_ad($order_id,$uid);
        }

        if(empty($ad_data) && !empty($order_id)){
            $ad_data = $this->charity_activity_ad($order_id);
        }

        if(empty($ad_data)){
            $ad_data = $this->get_default_share_ad();
        }

        $data = $this->convert_ad_data($ad_data);
        if(empty($data)){
            returnApiError('没有数据',$data);
        }else{
            returnApiSuccess($data);
        }
    }

    private function convert_ad_data($ad_data){
        $data = array();

        if(!empty($ad_data['info'])){
            $data['type'] = $ad_data['type'];
            switch ($ad_data["type"]){
                case "share":
                    $data['share_data'] = $ad_data['info'];
                break;
                case "redirect":
                    $data['redirect_data'] = $ad_data['info'];
                break;
            }
        }

        return $data;
    }


    /**
     * 默认支付完成弹窗广告信息
     * @author yangzhic
     * @return array
     */
    private function get_default_share_ad(){
        $data = C('BONUS_SHARE_INFO');
        $share_data = array();

        if(!empty($data)){
            $share_data = array(
                'ad_img'=>C('default_ad_img'),
                'time_line'=>array(
                    'title'=>$data['friends_content'],
                    'pic'=>$data['pic'],
                    'link'=>$data['link']
                ),
                'app_messages'=>array(
                    'title'=>$data['title'],
                    'desc'=>$data['content'],
                    'pic'=>$data['pic'],
                    'link'=>$data['link']
                )
            );
        }
        $data['type'] = "share";
        $data['info'] = $share_data;
        return $data;
    }

    /**
     * 公益广告信息
     * @author yangzhic
     * @param $order_id
     * @return array
     */
    private function charity_activity_ad($order_id){
        $data = array();

        $order_share_config = C('order_share_ad');

        if($order_share_config['status']==0){
            return $data;
        }

        $orderService = new OrderService();

        //获取订单信息
        $order_info = $orderService->getOrderInfoById($order_id);

        //获取订单实付金额
        $pay_fee = $order_info['order_amount'] + $order_info['surplus'] + $order_info['money_paid'];

        //获取订单中商品的商品id集合
        $goods_id_list = $orderService->getOrderGoodsId($order_id);

        //获取配置对应的商品id集合
        $goods_id_config = array_keys($order_share_config['shows']);

        if(!empty($goods_id_config)){
            //获取订单商品与配置的商品交集
            $goods_list = array_intersect($goods_id_config,$goods_id_list);
        }

        $share_data = array();

        if(empty($goods_list)){
            if($pay_fee>=99){
                $share_data = $order_share_config['default'];
            }
        }else{
            $max_price_goods_id = $orderService->getMaxPriceGoodsId($order_id,$goods_list);
            $share_data = $order_share_config['shows'][$max_price_goods_id];
        }

        if(!empty($share_data)){
            $data['type'] = "share";
            $data['info'] = $share_data;
        }

        return $data;
    }

    /**
     * 38活动广告信息
     * @author yangzhic
     * @param $order_id
     * @param $uid
     * @return array
     */
    private function women_day_ad($order_id,$uid){
        $data = array();
        $config = C('women_day_ad');

        $current_time = gmtime();
        $begin_time = local_strtotime('2018-03-06');
        $end_time = local_strtotime('2018-03-08');

        if(empty($order_id) || empty($uid)){
            return $data;
        }

        if($config['status']==0 || ($current_time<$begin_time || $current_time>$end_time)){
            return $data;
        }

        $orderService = new OrderService();

        //获取订单中商品的商品id集合
        $goods_id_list = $orderService->getOrderGoodsId($order_id);
        //获取配置对应的商品id集合
        $goods_id_config = $config['goods_id'];

        if(!empty($goods_id_config)){
            //获取订单商品与配置的商品交集
            $goods_list = array_intersect($goods_id_config,$goods_id_list);
            if(!empty($goods_list)){
                $is_new = $this->check_new_user($uid);
                $redirect_data = $config['shows'];
                $redirect_info = $redirect_data['redirect_info'][$is_new];
            }
            if(!empty($redirect_info)){
                $data['type'] = "redirect";
                $data['info'] = $redirect_info;
            }
        }
        return $data;
    }

    /**
     * 新用户
     * @author yangzhic
     * @param $uid
     * @return string
     */
    public function check_new_user($uid){
        $is_new = "new";
        $order_map = array('user_id'=>$uid,'order_status'=>array('in',array(OS_CONFIRMED,OS_SPLITED)),'pay_status'=>PS_PAYED);
        $order_count = M('order_info')->where($order_map)->count();
        if($order_count>1){
            $is_new = "old";
        }

        return $is_new;
    }
}    